package com.wwwarehouse.contract.release_supply_requirements;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.contract.bean.CanSeePublishResourseBean;
import com.wwwarehouse.contract.bean.LocalPublisgBean;
import com.wwwarehouse.contract.bean.SelectResourseBean;
import com.wwwarehouse.contract.common.ContractCommon;
import com.wwwarehouse.contract.core.ContractConstant;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/24 18:09
 * @描述 ${TODO}
 */
@Route(path = "/contract/PublishResourceListFragment")
public class PublishResourceListFragment extends BaseFragment implements View.OnClickListener {


    private View                    mView;
    private CustomViewPagerInternal mVpContain;
    private CirclePageIndicator     mPageIndicate;
    private Button                  mBtPublish;
    private StateLayout             mStateLayout;
    private SlideBarView            mSlideBarView;
    private List<Fragment>          mFragmentList;
    private BigDecimal              mBigUkid;
    private Bundle                  mBundleAsk;
    private Map                     mCanSeePublishMap;
    public static final int PAGE = 1;
    public static final int SIZE = 7;
    private PublishResourceListFragmentChild                    mPublishResourceListFragmentChild;
    private long                                                mBuId;
    private List<CanSeePublishResourseBean.TempPbResourcesBean> mForTempPbResourseList;
    private List<LocalPublisgBean>                              mTempPublishList;
    private Map                                                 mTempPublishMap;
    private Bundle                                              mPublishNumBundle;
    private Bundle                                              mSelectToCanSeePublishBundle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_publish_resourse_list, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initData() {
        ContractCommon.getInstance().setSeleteToHistory(false);
        mTempPublishMap = new HashMap();
        mTempPublishList = new ArrayList<>();
        mForTempPbResourseList = new ArrayList<>();
        ContractCommon.getInstance().setForTempPbResourseList(mForTempPbResourseList);
        mSelectToCanSeePublishBundle = getArguments();
        if (mSelectToCanSeePublishBundle != null) {

            mBuId = mSelectToCanSeePublishBundle.getLong("buId");
            mCanSeePublishMap = new HashMap();
            //            mBigUkid = new BigDecimal(mBuId);
            mCanSeePublishMap.put("buId", mBuId);
            mCanSeePublishMap.put("page", PAGE);
            mCanSeePublishMap.put("size", SIZE);
            mCanSeePublishMap.put("sort", "");
            mCanSeePublishMap.put("type", 0);
        }
        mFragmentList = new ArrayList<>();
        getCanSeePublishList();
        mPublishNumBundle = new Bundle();//总共发布多少条
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().post("HISTORY_BACK_SELECT");
    }

    public void getCanSeePublishList() {
        mStateLayout.showProgressView(true);//加载时候的状态
        NoHttpUtils.httpPost(ContractConstant.QUERY_PUBLISH_LIST, mCanSeePublishMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                switch (i) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {

                            SelectResourseBean selectResourseBean = JSON.parseObject(commonClass.getData().toString(), SelectResourseBean.class);
                            int total = selectResourseBean.getTotal();
                            if (total == 0) {
                                mStateLayout.showEmptyView(true);//请求回来为空
                                mStateLayout.setEmptyListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        getCanSeePublishList();
                                    }
                                });
                            } else {
                                final int page = (total % 7 == 0) ? total / 7 : (total / 7 + 1);
                                mFragmentList.clear();
                                for (int j = 0; j < page; j++) {
                                    mBundleAsk = new Bundle();
                                    newFragment(j);
                                    mPublishResourceListFragmentChild.setArguments(mBundleAsk);
                                    mFragmentList.add(mPublishResourceListFragmentChild);
                                }
                                mVpContain.setAdapter(new ContractFragmentPagerAdapter(getChildFragmentManager(), mFragmentList));
                                if (page <= 6) {
                                    mPageIndicate.setVisibility(View.VISIBLE);
                                    mSlideBarView.setVisibility(View.GONE);
                                    mPageIndicate.setViewPager(mVpContain, 0);
                                } else {
                                    mPageIndicate.setVisibility(View.GONE);
                                    mSlideBarView.setVisibility(View.VISIBLE);
                                    mSlideBarView.setTotalPage(page);
                                    mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                                        @Override
                                        public void onSlideChange(int i) {
                                            mVpContain.setCurrentItem(page, false);
                                        }
                                    });
                                    mSlideBarView.setViewPagerInternal(mVpContain);
                                }
                            }
                        } else {
                            mStateLayout.showSystemView(commonClass.getMsg(), true);
                            mStateLayout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    getCanSeePublishList();
                                }
                            });
                        }
                        break;
                    case 1:
                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog("Exception" + e.toString());
            }


        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);//网络问题
            mStateLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getCanSeePublishList();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    private void newFragment(int j) {
        mPublishResourceListFragmentChild = new PublishResourceListFragmentChild();
        mBundleAsk.putInt("page", j + 1);
        mBundleAsk.putLong("buId", mBuId);
    }

    private void initView() {

        mStateLayout = findView(mView, R.id.sl);
        mVpContain = findView(mView, R.id.cvpi);
        mPageIndicate = findView(mView, R.id.cpi);
        mSlideBarView = findView(mView, R.id.sbv_slide);
    }

    @Override
    public void setTitle() {
        if(peekFragment() instanceof PublishResourceListFragment)
        mActivity.setTitle(mActivity.getResources().getString(R.string.published_re));
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {
    }
}
